// pages/payment/index.js
import {
  getDeliverPrice,
  ApiGetaddressList,
  ApiMedicineOrder,
  getLogisticsFee
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexData: '0',
    dataV: [{
      label: '快递到家',
      value: '0'
    }, {
      label: '到院自取',
      value: '1'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    /*  medicine = [{
       "id": 4,
       "count": "1",
       "price": "128.00",
       "name": "维塔利胶囊"
     }] & sum = 128 */
    this.setData({
      medicine: JSON.parse(options.medicine),
      sum: options.sum,
      advice_id: options.medicId
    })
    this.getAddressList()
  },



  dataClick(e) {
    console.log(e)
    let indexData = e.currentTarget.dataset.v.value
    this.setData({
      indexData
    })
    // this.getFee()

  },



  getAddressList() {
    ApiGetaddressList().then(res => {
      let data = res.data.find(item => {
        return (item.use == 1)
      })
      this.setData({
        defaultData: data
      })
    }).then((_) => {
      // this.getFee()
    })
  },

  //获取物流费用
  getFee() {
    let areaArr = this.data.defaultData.area.split('-')
    let data = {
      province: areaArr[0],
      city: areaArr[1],
      address: this.data.defaultData
    }
    getDeliverPrice(data).then(res => {
      if (res.data) {
        this.setData({
          courierFee: res.data.fee,
        })
        if (this.data.indexData == '1') {
          this.setData({
            sum: Number(this.data.sum) - (Number(this.data.courierFee))
          })
        } else {
          this.setData({
            sum: Number(this.data.sum) + (Number(this.data.courierFee))
          })
        }
      }
    })
  },

  //跳转到对应的邮寄地址
  handleArea() {
    wx.navigateTo({
      url: '/pages/addressManage/index?data=b',
    })
  },

  //去支付
  payButton() {
    let data = []
    this.data.medicine.forEach(item => {
      data.push({
        id: item.id,
        count: item.count
      })
    })
    if (this.data.dataV[this.data.indexData].label == 0){
      var query = {
        medicine: data,
        name: this.data.defaultData.name ,
        area: this.data.defaultData.area ,
        area_detail: this.data.defaultData.area_detail ,
        phone: this.data.defaultData.phone ,
        advice_id: this.data.advice_id,
        order_type: this.data.dataV[this.data.indexData].label
      }
    }else {
      var query = {
        medicine: data,
       
        advice_id: this.data.advice_id,
        order_type: this.data.dataV[this.data.indexData].label
      }
    }
    ApiMedicineOrder(query).then(res => {
      wx.navigateTo({
        url: `/pages/drugPay/index?order=${JSON.stringify(res.data.order)}`,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    console.log(currPage)
    if (currPage.data.infoContact) {
      this.setData({
        defaultData: currPage.data.infoContact
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})